/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 
package buco.http;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.service.http.HttpContext;

public class WebClientHttpContext implements HttpContext {

	String m_root; 
	
	public WebClientHttpContext(String root) {
		m_root = root; 
	}
	
	@Override
	public String getMimeType(String name) {
		name = name.toLowerCase();
		if(name.equals("/") || name.isEmpty() || name.endsWith(".html"))  {
			return "text/html";
		}			
		else if(name.endsWith(".css")) {
			return "text/css"; 
		}
		else if(name.endsWith(".js")) {
			return "application/javascript"; 
		}
		return null; 
	}

	@Override
	public URL getResource(String name) {						
		if(name.equals("/") || name.isEmpty())  { 
			name = "index.html";			
		}
		else if(name.equals("getDeployment")) {
			
		}
		
		try {
			return new File(m_root, name).toURI().toURL();
		} catch (MalformedURLException e) {
			System.out.println("invalid resource request: " + name); 
			return null;
		}		
	}

	@Override
	public boolean handleSecurity(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
		// No security right now 
		return true;
	}

}
